const { network } = require("hardhat")
const { devlopmentChains, networkConfig, LOCK_TIME, CONFIRMATIONS } = require("../helper-hardhat-config")

module.exports = async({getNamedAccounts, deployments}) => {
    //firstAccount在hardhat.config.js的namedAccounts定义
    const {firstAccount} = (await getNamedAccounts())
    const {deploy} = deployments

    let dataFeedAddr
    let confirmations
    if (devlopmentChains.includes("hardhat")) {
        const mockDeployment = await deployments.get("MockV3Aggregator")
        dataFeedAddr = mockDeployment.address
        confirmations = 0
    } else {
        dataFeedAddr = networkConfig[network.config.chainId].ethUdsDataFeed
        confirmations = CONFIRMATIONS
    }

    //deploy FundMe contract
    //from: 合约部署者
    //args：合约构造函数入参
    //log：是否打印日志
    //waitConfirmations: 等待n个区块
    const fundMe = await deploy("FundMe", {
        from: firstAccount,
        args: [LOCK_TIME, dataFeedAddr],
        log: true,
        waitConfirmations: confirmations
    })

    //verify fundMe
    if (hre.network.config.chainId == 11155111 && process.env.ETHERSCAN_API_KEY) {
        await hre.run("verify:verify", {
            address: fundMe.address,
            constructorArguments: [LOCK_TIME, dataFeedAddr]
        })
    } else {
        console.log("Network is not sepolia, verification stkipped...")
    }
}

module.exports.tags = ["all", "fundme"]